/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.tesam.gedeon.service.impl;

import com.tesam.gedeon.dao.ServiceDao;
import com.tesam.gedeon.entity.Service;
import com.tesam.gedeon.service.ServiceService;
import java.text.SimpleDateFormat;
import java.util.List;
import org.primefaces.json.JSONArray;
import org.primefaces.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;

/**
 *
 * @author joelpacheco
 */
@org.springframework.stereotype.Service
public class ServiceServiceImpl implements ServiceService {

    @Autowired
    private ServiceDao serviceDao;

    @Override
    public int save(Service service) {
        return serviceDao.save(service);
    }

    @Override
    public int delete(Service service) {
        return serviceDao.delete(service);
    }

    @Override
    public int update(Service service) {
        return serviceDao.update(service);
    }

    @Override
    public Service getById(int serviceId) {
        return serviceDao.getById(serviceId);
    }

    @Override
    public List<Service> getAll() {
        return serviceDao.getAll();
    }

    @Override
    public List<Service> getAllByLimit(int startLimit, int endLimit, String dataToSort, String directionOfOrder) {
        return serviceDao.getAllByLimit(startLimit, endLimit, dataToSort, directionOfOrder);
    }

    @Override
    public List<Service> searchByName(Service service, int satartLimit, int endLimit) {
        return serviceDao.searchByName(service, satartLimit, endLimit);
    }

    @Override
    public int getCountSearchByName(Service service) {
        return serviceDao.getCountSearchByName(service);
    }

    @Override
    public int getCountTotal() {
        return serviceDao.getCountTotal();
    }

    @Override
    public String getJsonDataFromArrayList(List<Service> listaDeService, int cantidadRegistros, int moduleId) {

        JSONArray array = null;
        JSONObject json = null;

        SimpleDateFormat format = null;

        try {

            json = new JSONObject();
            array = new JSONArray();

            if (listaDeService.size() > 0) {

                format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
                // Date date = sdf.parse("2004-07-24 09:45:52.189");

                for (Service service : listaDeService) {
                    JSONArray arrayPut = new JSONArray();

                    arrayPut.put(service.getId());
                    arrayPut.put(service.getCode());
                    arrayPut.put(service.getName());
                    arrayPut.put(service.getProvider().getName());

                    if (service.getDateCreated() != null) {
                        arrayPut.put((format.format(service.getDateCreated())));
                    } else {
                        arrayPut.put(service.getDateCreated());
                    }

                    // arrayPut.put("<a href=\"#delete\" data-toggle=\"modal\" class=\" btn yellow\" value=\""+rol.getId()+"\">Eliminar</a>");
                    arrayPut.put("<a class=\"btn mini blue\" href=\"/GedeonMvc/service/view?id=" + service.getId() + "&module=" + moduleId + "\">"
                            + "<i class=\"icon-share\"></i>View</a>"
                            + " <a class=\"btn mini green\" href=\"/GedeonMvc/service/edit?id=" + service.getId() + "&module=" + moduleId + "\">"
                            + "<i class=\"icon-edit\"></i>Editar</a>"
                            + " <a href=\"#delete\" data-toggle=\"modal\" class=\"btn mini red\" value=\"" + service.getId() + "\"><i class=\"icon-trash\"></i>Eliminar</a>");
                    array.put(arrayPut);
                }
            }

            json.put("iTotalRecords", cantidadRegistros);
            json.put("iTotalDisplayRecords", cantidadRegistros);
            json.put("aaData", array);

            return json.toString();
        } catch (Exception e) {
            System.out.println("Exception getJsonDataFromArrayList:" + e.getMessage());
            return null;
        }

    }
}
